package cn.jietuo.juc.c_007_reentrant;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author zhangx & jietuo_zx@163.com
 * @version 1.0
 * @date 2021/1/11
 * @description:
 */
public class T04_CountDownLatch_02 {

    private static AtomicInteger atomicInteger = new AtomicInteger(0);

    public static void main(String[] args) {
        CountDownLatch countDownLatch = new CountDownLatch(3);
        new Thread(() -> {
            int result = 0;
            for (int j = 0; j < 10000; j++) {
                result += j;
            }
            countDownLatch.countDown();
            int i = atomicInteger.incrementAndGet();
            System.out.println("Thread1--- i = " + i + ", result = " + result);
        }).start();

        new Thread(() -> {
            int result = 0;
            for (int j = 0; j < 10000; j++) {
                result += j;
            }
            countDownLatch.countDown();
            int i = atomicInteger.incrementAndGet();
            System.out.println("Thread2--- i = " + i + ", result = " + result);

        }).start();

        new Thread(() -> {
            int result = 0;
            for (int j = 0; j < 10000; j++) {
                result += j;
            }
            countDownLatch.countDown();
            int i = atomicInteger.incrementAndGet();
            System.out.println("Thread3--- i = " + i + ", result = " + result);
        }).start();


        new Thread(() -> {
            int result = 0;
            for (int j = 0; j < 10000; j++) {
                result += j;
            }
            countDownLatch.countDown();
            int i = atomicInteger.incrementAndGet();
            System.out.println("Thread4--- i = " + i + ", result = " + result);
        }).start();

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("useCountDownLatch end");
    }
}
